/*
 * ConnectionFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package gov.nist.applet.phone.ua.gui;

import gov.nist.applet.phone.ua.RegisterStatus;
import gov.nist.applet.phone.ua.presence.Subscriber;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

import com.dtec.common.util.SettingUtil;

/**
 * 
 * @author __USER__
 */
public class ConnectionFrame extends javax.swing.JFrame {

	private NISTMessenger mainFrame = null;
	private Map map = null;
	private ConnectionXmlManager xmlManager = null;
	private String path = null;
	private DefaultMutableTreeNode node = null;
	private DefaultMutableTreeNode root = null;
	private DefaultMutableTreeNode friends = null;
	private DefaultMutableTreeNode relationship = null;
	private DefaultTreeModel model = null;

	/** Creates new form ConnectionFrame */
	public ConnectionFrame(int x, int y, int width, int height,
			NISTMessenger mainFrame) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception exe) {
			exe.printStackTrace();
		}
		this.setBounds(new java.awt.Rectangle(x, y, x, y));
		this.setSize(520, 320);
		this.mainFrame = mainFrame;
		this.path = "xmlSource/ConnectionInfo.xml";
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		connectTree = new javax.swing.JTree();
		connectNumTextField = new javax.swing.JTextField();
		addToFriendsBtn = new javax.swing.JButton();
		addToRelationshipBtn = new javax.swing.JButton();
		connectNumTextField = new javax.swing.JTextField();
		addToFriendsBtn = new javax.swing.JButton();
		addToRelationshipBtn = new javax.swing.JButton();

		// DefaultMutableTreeNode root = new DefaultMutableTreeNode("��ڵ�");
		// node = new DefaultMutableTreeNode("�ڵ�1");
		// root.add(node);
		// node = new DefaultMutableTreeNode("�ڵ�2");
		// root.add(node);
		//
		node = null;
		root = new DefaultMutableTreeNode("��ϵ��");
		friends = new DefaultMutableTreeNode("����");
		relationship = new DefaultMutableTreeNode("����");
		// DefaultMutableTreeNode others = new DefaultMutableTreeNode("����");
		// friends.add(new DefaultMutableTreeNode("182"));

		// root.add(others);
		// child1.add (child11); child11.add (child111);
		xmlManager = new ConnectionXmlManager(path);
		map = xmlManager.readXml();
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			if (value.toString().equals("friend")) {
				friends.add(new DefaultMutableTreeNode(key.toString()));
			} else {
				relationship.add(new DefaultMutableTreeNode(key.toString()));
			}
		}
		root.add(friends);
		root.add(relationship);

		model = new DefaultTreeModel(root);

		connectTree = new javax.swing.JTree(model);
		connectTree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);

		connectTree.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					javax.swing.JTree jTree = (javax.swing.JTree) e.getSource();
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree
							.getAnchorSelectionPath().getLastPathComponent();
					if (node.isLeaf() && node.toString().matches("\\d+")) {
						if (!mainFrame.getSipMeetingManager()
								.getRegisterStatus()
								.equalsIgnoreCase(RegisterStatus.REGISTERED)) {
							JOptionPane
									.showMessageDialog(
											mainFrame,
											"You must be registered to add a new contact",
											"Contact Error",
											JOptionPane.ERROR_MESSAGE);
							return;
						}
						String contactAddress = node.toString()
								+ "@"
								+ SettingUtil.getDefaultAccount(path)
										.getProxyaddress();
						if (contactAddress != null) {
							if (contactAddress.indexOf("@") != -1) {
								Subscriber subscriber = new Subscriber(
										contactAddress);
								mainFrame.getSipMeetingManager()
										.getPresentityManager()
										.addSubscriber(subscriber);
								mainFrame.getSipMeetingManager().sendSubscribe(
										contactAddress);
								mainFrame.getControllerMeeting().addContact(
										contactAddress);
							} else {
								JOptionPane.showMessageDialog(mainFrame,
										"the contact has not been added",
										"Contact Error",
										JOptionPane.ERROR_MESSAGE);
							}
						}
					}
				}
			}
		});

		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

		jScrollPane1.setViewportView(connectTree);

		addToFriendsBtn.setText("addToFriends");
		addToFriendsBtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addToFriendsBtnActionPerformed(evt);
			}
		});

		addToRelationshipBtn.setText("addToRelationship");
		addToRelationshipBtn
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						addToRelationshipBtnActionPerformed(evt);
					}
				});

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(jScrollPane1,
						org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 266,
						Short.MAX_VALUE)
				.add(org.jdesktop.layout.GroupLayout.TRAILING,
						layout.createSequentialGroup()
								.add(addToFriendsBtn,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										125,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.add(addToRelationshipBtn))
				.add(layout
						.createSequentialGroup()
						.add(connectNumTextField,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								143,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(123, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(jScrollPane1,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								348,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.add(connectNumTextField,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								34,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.UNRELATED)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(addToRelationshipBtn,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										35,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(addToFriendsBtn,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										35,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	// GEN-END:initComponents

	private void addToRelationshipBtnActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		if (!connectNumTextField.getText().equals("")) {
			String connectAddress = connectNumTextField.getText();
			ConnectionXmlManager xmlManager = new ConnectionXmlManager(path);
			xmlManager.addXmlCode(connectAddress, "relationship");
		}
	}

	private void addToFriendsBtnActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		if (!connectNumTextField.getText().equals("")) {
			String connectAddress = connectNumTextField.getText();
			ConnectionXmlManager xmlManager = new ConnectionXmlManager(path);
			xmlManager.addXmlCode(connectAddress, "friend");
		}
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				// new ConnectionFrame(300, 200, 300, 200).setVisible(true);
			}
		});
	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton addToFriendsBtn;
	private javax.swing.JButton addToRelationshipBtn;
	private javax.swing.JTextField connectNumTextField;
	private javax.swing.JTree connectTree;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration//GEN-END:variables

}